* Load the data file -----------------------------------------------------------

	if "$individual_run" != "Yes" {
	di "Program Stopped: Make sure you load the parameters in master.do"
	e
	}

	use $interim_data, clear

* Attrition table CALCULATION --------------------------------------------------
	
	preserve
		
		* Sample restriction
		drop if year_month < 202101 // Consider 2021 January onward
		
		* Reshape the data 
		gl vars_i ///
			employee_id treatment ///
			dmale dblack dwhite dhispanic strata no_endline
			
		keep $vars_i missing year_month	
		
		reshape wide missing, i($vars_i) j(year_month) 
		
		gen i = 1
		
		* Officers with empty values are those where months are in training
		recode missing* (. = 0)
				
		* Denominators: Police officers who have data up to a given month
		egen missing_any_12 = rowmin(missing202101 - missing202102)
		egen missing_any_08 = rowmin(missing202101 - missing202106)
		egen missing_any_04 = rowmin(missing202101 - missing202110)
		
		// 0 for these variable mean there is at least one month with no missing
		 
		* Attrition definition
			
			* Missing any month since 2021 01
			egen attrition    = rowtotal(missing202101 - missing202202)
			gen att_any = (attrition !=0)
		
				* Missing all months in a given time frame
				* 12: Have data in any month between 2021 01 - 2021 02
				* But does not have any data between 2021 03 - 2022 02
			
			egen attrition_12 = rowtotal(missing202103 - missing202202)
			gen att_12 = 0  if missing_any_12 == 0
			replace att_12 = 1 if (attrition_12 == 12  & missing_any_12 == 0)
			
			egen attrition_08 = rowtotal(missing202107 - missing202202)
			gen att_08 = 0  if missing_any_08 == 0
			replace att_08 = 1 if (attrition_08 == 8 & missing_any_08 == 0)
			
			egen attrition_04 = rowtotal(missing202111 - missing202202)
			gen att_04 = 0 if missing_any_04 == 0
			replace att_04 = 1 if (attrition_04 == 4  & missing_any_04 == 0)
			
			* Run analysis
			foreach outcome in att_any att_12 att_08 att_04 no_endline {
				bal_row `outcome' i A_`outcome'
			}
		
	restore
	
* Attrition TABLE WRITING ------------------------------------------------------

	* Table B3
	table_start_customized_c ///
	${report_folder}/outcome_attrition.txt 4 ///
	"Attrition" ///
	"& \textbf{Control Mean} & \textbf{Treatment Mean} & \textbf{Difference} & \textbf{N} \\ " 150 
	midrule
		
		bal_row_write 	A_att_any 		"Attrition"						hidesd
		bal_row_write 	A_att_12 		"Attrition (12 months)"			hidesd
		bal_row_write 	A_att_08 		"Attrition (8 months)"			hidesd
		bal_row_write 	A_att_04 		"Attrition (4 months)"			hidesd
		bal_row_write 	A_no_endline 	"Attrition (Endline survey)" 	hidesd
		
	table_end "$admin_attrition_end" outcome_att
